package com.youdelu.road;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.youdelu.road.adapter.ListAdapter;
import com.youdelu.road.bean.Attr;
import com.youdelu.road.bean.BgAttr;
import com.youdelu.road.utils.AppConfig;
import com.youdelu.road.utils.BroadcastManager;
import com.youdelu.road.utils.Util;

import java.util.ArrayList;
import java.util.List;

public class MapBgListActivity extends AppCompatActivity  implements View.OnClickListener,ListAdapter.OnClickInterface {



    public static final String SAVE_BG_IMG = "save_bg_img";
    private Context mContext;
    private DbUtils db ;
    private TextView add_bg ;
    private ListView list ;
    private ListAdapter adapter;
    private List<BgAttr> bgAttrs = new ArrayList<>();
    private int  bg_id  = 0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_list);
        mContext = this ;
        db = Util.db;
        add_bg = findViewById(R.id.add_bg);
        list = findViewById(R.id.list);
        adapter = new ListAdapter(mContext,this,bgAttrs);
        list.setAdapter(adapter);
        add_bg.setOnClickListener(this);
        String bgid = AppConfig.getInstance(mContext).get(MainActivity.IMG_BGID);
        if(bgid!=null){
            bg_id = Integer.parseInt(bgid);
        }
        initBroad();
        reflushAdapter();
    }

    public void initBroad(){
        BroadcastManager.getInstance(mContext).addAction(SAVE_BG_IMG, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String name = intent.getStringExtra("name");
                String path = intent.getStringExtra("path");
                int id = intent.getIntExtra("id",-1);
                BgAttr attr = new BgAttr();
                if(id!=-1){
                    attr.setId(id);
                }
                if(name.equals("-1#")){
                    try {
                        db.delete(attr);
                    }catch (Exception e){

                    }
                    Toast.makeText(mContext,"已删除",Toast.LENGTH_SHORT).show();
                }else{
                    attr.setName(name);
                    attr.setPath(path);
                    int dd = attr.getId();
                    try {
                        db.saveOrUpdate(attr);
                        if(dd==0){
                            Util.restoreAttr(attr.getId());
                        }
                    }catch (Exception e){

                    }
                }
                reflushAdapter();
            }
        });
    }
    public void reflushAdapter(){
        try {
            bgAttrs = db.findAll(BgAttr.class);
            adapter.update(bgAttrs,bg_id);
        }catch (Exception e){

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_bg:
                Intent intent = new Intent();
                intent.setClass(mContext, AddMapBgActivity.class);
                startActivity(intent);
                break;
        }

    }


    @Override
    protected void onDestroy() {
        BroadcastManager.getInstance(mContext).destroy(SAVE_BG_IMG);
        super.onDestroy();
    }

    @Override
    public void open(BgAttr at) {
        Attr attr = new Attr();
        attr.setPath(at.getPath());
        attr.setBgId(at.getId());
        bg_id = at.getId();
        reflushAdapter();
        BroadcastManager.getInstance(mContext).sendBroadcast(MainActivity.Add_MAP,attr);
    }

    @Override
    public void del(BgAttr attr) {
        try{

            db.delete(Attr.class, WhereBuilder.b("bgId","=",attr.getId()));

            if(attr.getId()!=0){
                db.delete(attr);
            }else{
                Util.restoreAttr(0);
                BroadcastManager.getInstance(mContext).sendBroadcast(MainActivity.REFLUSH_MAP,attr.getId(),null,null);
            }
        }catch (Exception e){

        }
        reflushAdapter();
    }
}
